import java.io.IOException;
import java.util.Comparator;

/**
 * StudentProcessor 类 - 学生信息处理系统
 * 实现学生数据的读取、排序、过滤、转换和输出
 */
public class StudentProcessor {

    /**
     * 处理学生信息的主方法
     */
    public static void processStudentData() {
        try {
            // 1. 读取学生数据
            System.out.println("1. 读取学生数据...");
            DataContainer<Student> students = readStudentsFromFile("students.txt");
            System.out.println("读取到 " + students.size() + " 名学生");

            // 2. 创建 DataContainer
            DataContainer<Student> studentContainer = new DataContainer<>();
            for (int i = 0; i < students.size(); i++) {
                studentContainer.add(students.get(i));
            }

            // 3. 按成绩降序排序
            System.out.println("2. 按成绩降序排序...");
            DataContainer<Student> sortedStudents = GenericConverter.sort(studentContainer,
                    Comparator.comparing(Student::getScore).reversed());

            // 4. 过滤高分学生（成绩 > 80）
            System.out.println("3. 过滤高分学生（成绩 > 80）...");
            DataContainer<Student> highScoreStudents = sortedStudents.filter(s -> s.getScore() > 80);
            System.out.println("高分学生数量: " + highScoreStudents.size());

            // 5. 转换为姓名容器
            System.out.println("4. 转换为姓名容器...");
            DataContainer<String> nameContainer = GenericConverter.convert(highScoreStudents,
                    Student::getName);

            // 6. 写入结果文件
            System.out.println("5. 写入结果文件...");
            writeNamesToFile(nameContainer, "top_students_names.txt");
            System.out.println("处理完成！结果已保存到 top_students_names.txt");

        } catch (IOException e) {
            System.err.println("文件操作错误: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 从文件读取学生数据
     * 
     * @param filePath 文件路径
     * @return 学生数据容器
     * @throws IOException 文件读取异常
     */
    private static DataContainer<Student> readStudentsFromFile(String filePath) throws IOException {
        DataContainer<Student> students = new DataContainer<>();

        try (java.io.BufferedReader reader = new java.io.BufferedReader(
                new java.io.FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.trim().isEmpty() || line.startsWith("//")) {
                    continue; // 跳过空行和注释行
                }

                String[] parts = line.split(",");
                if (parts.length == 4) {
                    String id = parts[0].trim();
                    String name = parts[1].trim();
                    int age = Integer.parseInt(parts[2].trim());
                    double score = Double.parseDouble(parts[3].trim());

                    students.add(new Student(id, name, age, score));
                }
            }
        }

        return students;
    }

    /**
     * 将姓名容器写入文件
     * 
     * @param nameContainer 姓名容器
     * @param filePath      文件路径
     * @throws IOException 文件写入异常
     */
    private static void writeNamesToFile(DataContainer<String> nameContainer, String filePath) throws IOException {
        try (java.io.BufferedWriter writer = new java.io.BufferedWriter(
                new java.io.FileWriter(filePath))) {
            for (int i = 0; i < nameContainer.size(); i++) {
                writer.write(nameContainer.get(i));
                writer.newLine();
            }
        }
    }

    /**
     * 主方法
     * 
     * @param args 命令行参数
     */
    public static void main(String[] args) {
        processStudentData();
    }
}
